package de.baumann.browser.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import de.baumann.browser.R;

/**
 * @author:
 * @date：2018/9/18
 * @discription: 操作菜单适配器
 **/
public class OperateAdapter extends RecyclerView.Adapter {

    private int[] operateNames = {R.string.add_bookmark, R.string.bookmark_history, R.string.refresh, R.string.about_odin,R.string.feedback,R.string.tab_windows};
    private int[] operateIcons = {R.drawable.menu_icon_shoucang, R.drawable.menu_icon_shuqian, R.drawable.menu_icon_shuaxin,
            R.drawable.menu_icon_about,R.drawable.menu_icon_opinion,R.drawable.menu_icon_windows};

    AdapterView.OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_operate, parent, false);
        return new OperateViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        OperateViewHolder operateView = (OperateViewHolder) holder;
        operateView.operateName.setText(operateNames[position]);
        operateView.operateIcon.setImageResource(operateIcons[position]);
        operateView.itemView.setOnClickListener(view -> {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(null, null, position, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return operateNames.length;
    }

    class OperateViewHolder extends RecyclerView.ViewHolder {

        ImageView operateIcon;
        TextView operateName;

        public OperateViewHolder(View itemView) {
            super(itemView);
            operateIcon = itemView.findViewById(R.id.img_operate_icon);
            operateName = itemView.findViewById(R.id.tv_operate_name);
        }
    }
}
